﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace BanVeXemPhim_Presentation
{
    public class LichChieuTuanDAO:DataProvider
    {

        public LichChieuTuanDTO[] DanhSach10LichChieuGanNhat()
        {
            connect();
            string query = "select TOP 10 * from lichchieutuan order by idTuan desc";
            adapter = new SqlDataAdapter(query, connection);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet);
            DataTable dataTable = dataSet.Tables[0];
            int n = dataTable.Rows.Count;
            LichChieuTuanDTO[] lichChieuTuanDtos = new LichChieuTuanDTO[n];
            for (int i = 0; i < n; i++)
            {
                lichChieuTuanDtos[i] = (LichChieuTuanDTO)GetDataFromDataRow(dataTable, i);
            }
            return lichChieuTuanDtos;

            return null;
        }
        protected override object GetDataFromDataRow(System.Data.DataTable dt, int i)
        {
            LichChieuTuanDTO lichChieuTuanDto=new LichChieuTuanDTO();

            lichChieuTuanDto.IdTuan = Convert.ToInt32(dt.Rows[i]["IdTuan"].ToString());
            lichChieuTuanDto.NgayBatDau = Convert.ToDateTime(dt.Rows[i]["NgayBatDau"].ToString());
            lichChieuTuanDto.NgayKetThuc = Convert.ToDateTime(dt.Rows[i]["NgayKetThuc"].ToString());

            return lichChieuTuanDto;
        }

        public void ThemLichChieuTuan(LichChieuTuanDTO lichChieuTuanDto)
        {
            connect();
            string query =
                "insert into lichchieutuan(ngaybatdau,ngayketthuc) values(convert(datetime,'"+lichChieuTuanDto.NgayBatDau.ToShortDateString()+"',103),convert(datetime,'"+lichChieuTuanDto.NgayKetThuc.ToShortDateString()+"',103))";
            executeNonQuery(query);
        }

        public bool KiemTraTruocKhiThem(LichChieuTuanDTO lichChieuTuanDto)
        {
            connect();
            string query = "select * from lichchieutuan where  ngayketthuc >= convert(datetime,'"+lichChieuTuanDto.NgayBatDau+"',103)";
            adapter = new SqlDataAdapter(query, connection);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet);
            DataTable dataTable = dataSet.Tables[0];
            int n = dataTable.Rows.Count;
            if (n > 0)
                return false;
            return true;
        }

        public void HuyLichChieuTuan(LichChieuTuanDTO _LichTuanHienTai)
        {
            connect();
            string query = "delete from xuatchieu where idtuan="+_LichTuanHienTai.IdTuan;
            executeNonQuery(query);
            query = "delete from lichchieutuan where idtuan=" + _LichTuanHienTai.IdTuan;
            executeNonQuery(query);
        }
    }
}
